/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tsb.UI.PopupMenus;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.border.BevelBorder;
import tsb.UI.PanelArrastrar.BotonPersonalizado;

/**
 *
 * @author Fede
 */
public class PopupMenu extends JPopupMenu  {
    
    public JPopupMenu popup;
    private BotonPersonalizado botton; 
    private boolean ban=false; //para saber si el popupmenu fue agregado al boton
    
    public PopupMenu(BotonPersonalizado b)
    {
        popup = new JPopupMenu();
        ActionListener menuListener = new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                
                
                if(event.getActionCommand().compareTo("Borrar")==0)
                {
                    botton.getPanelPrincipal().eliminarBoton(botton);
                    return;
                }
                if(event.getActionCommand().compareTo("Abrir")==0)
                {
                   // botton.mostrarVentana();
                }
                     
                
            }
        };
        
        JMenuItem item;
        popup.add(item = new JMenuItem("Borrar", new ImageIcon(getClass().getResource("/tsb/UI/Imagenes/eliminar16x16.png"))));
        item.setHorizontalTextPosition(JMenuItem.RIGHT);
        item.addActionListener(menuListener);
        popup.add(item = new JMenuItem("Abrir", new ImageIcon(getClass().getResource("/tsb/UI/Imagenes/action_print.gif"))));
        item.setHorizontalTextPosition(JMenuItem.RIGHT);
        item.addActionListener(menuListener);
//        popup.add(item = new JMenuItem("Right", new ImageIcon(getClass().getResource("/tsb/UI/Imagenes/action_save.gif"))));
//        item.setHorizontalTextPosition(JMenuItem.RIGHT);
//        item.addActionListener(menuListener);
//        popup.add(item = new JMenuItem("Full", new ImageIcon(getClass().getResource("/tsb/UI/Imagenes/icon_monitor_pc.gif"))));
//        item.setHorizontalTextPosition(JMenuItem.RIGHT);
//        item.addActionListener(menuListener);
//        popup.addSeparator();
//        popup.add(item = new JMenuItem("Settings . . ."));
//        item.addActionListener(menuListener);
//
//        popup.setLabel("Justification");
        popup.setBorder(new BevelBorder(BevelBorder.RAISED));
        popup.setVisible(false);
        
        this.botton=b;

        b.addMouseListener(new MousePopupListener());
    }
    
    
    class MousePopupListener extends MouseAdapter {
    public void mousePressed(MouseEvent e) {
      
        if(e.getButton()==3)
        {
            checkPopup(e);
        }
    }

//    public void mouseClicked(MouseEvent e) {
//     // checkPopup(e);
//    }
//
//    public void mouseReleased(MouseEvent e) {
//    //  checkPopup(e);
//    }

    private void checkPopup(MouseEvent e) {
        if(ban==false)
        {
            botton.add(popup);
            ban=true;
        }
//      if (e.isPopupTrigger()) {
        popup.show(botton, e.getX(), e.getY());
//      }
      //   botton.add(popup);
        
        
    }
  }
    
}
